<template>
  <div class="submit-dialog"
       @close="$emit('update:isShowModal', false)">
    <div style="line-height: 60px;">
      <div style="display: inline-block;">
        <span style="font-size: 14px; font-weight: 600; color: #666;">Language :</span>
      </div>&nbsp;&nbsp;
      <div style="display: inline-block;">
        <el-select v-model="language">
          <el-option v-for="(lan, index) in LANGUAGE_HASH" :key="index" :label="lan"
                     :value="index"></el-option>
        </el-select>
      </div>
    </div>
    <div style="margin-top: 10px;">
      <el-input v-model="textArea" type="textarea" :rows="20"></el-input>
    </div>
    <span slot="footer">
      <el-button @click="$emit('update:isShowModal', false)">Cancel</el-button>
      <el-button type="primary" @click="SubmitCode">Submit</el-button>
    </span>
  </div>
</template>

<script>
import {mapState} from "vuex";
import {JUDGE_OPTIONS} from "../utils/constants";
import storage from "../utils/storage";
import {uploadSubmission} from "@/network/submission";

export default {
  props: {
    title: {
      type: String,
      default: "",
    },
    uploadUrl: {
      type: String,
      default: "",
    },
    redirectPath: {
      type: String,
      default: "",
    },
  },
  mounted() {
    this.language = storage.get("language");
  },
  data() {
    return {
      textArea: "",
      language: 1,
      LANGUAGE_HASH: JUDGE_OPTIONS.LANGUAGE_HASH,
    };
  },
  computed: {
    ...mapState(["modalStatus"]),
  },
  methods: {
    SubmitCode: function () {
      if (this.language == null) {
        this.$message.warning("Please select a language first");
      } else if (!this.textArea) {
        this.$message.warning("Code could't be empty");
      } else {
        uploadSubmission(
            this.language,
            this.$route.params.problemId,
            this.$route.params.contestId,
            this.textArea
        ).then((res) => {
          if (res.status === 200) {
            const pid = this.$route.params.problemId;
            const cid = this.$route.params.contestId;
            this.$message.success("提交成功");
            this.$emit("update:isShowModal", false);
            if (typeof (cid) === 'undefined' || cid === -1) {
              this.$router.push({
                name: "submissionList",
                params: {
                  contestId: cid,
                },
                query: {
                  problemId: pid,
                },
              }).catch(error => {
                console.log(error)
              })
            } else {
              this.$router.push({
                name: "contest-submission-list",
                params: {
                  contestId: cid,
                }
              }).catch(error => {
                console.log(error)
              })
            }
          } else {
            this.$message.error("提交失败，错误信息：" + res.message)
          }
        });
      }
    },
  }
};
</script>

<style>
.submit-dialog {
  margin-top: 10px;
}
</style>
